package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hpplay.sdk.source.api.LelinkPlayerInfo;
import com.hpplay.sdk.source.api.LelinkSourceSDK;
import com.stark.cast.screen.CastScreenManager;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.VideoAdapter;
import flc.ast.bean.MyVideoBean;
import flc.ast.databinding.ActivitySelVideoBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.basic.utils.UriUtil;

public class SelVideoActivity extends BaseAc<ActivitySelVideoBinding> {

    private VideoAdapter videoAdapter;
    private List<MyVideoBean> listShow = new ArrayList<>();
    private List<MyVideoBean> listSel = new ArrayList<>();//选择的集合
    private CastScreenManager mCastScreenManager;


    @Override
    protected int onCreate() {
        return R.layout.activity_sel_video;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelVideoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivProVideoStart.setOnClickListener(this);
        mDataBinding.rvSelVideoList.setLayoutManager(new GridLayoutManager(mContext, 2));
        videoAdapter = new VideoAdapter();
        videoAdapter.setSel(true);
        videoAdapter.setEdit(true);
        mDataBinding.rvSelVideoList.setAdapter(videoAdapter);
        videoAdapter.setOnItemClickListener(this);
        mCastScreenManager=CastScreenManager.getInstance();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivProVideoStart:
                ProVideoActivity.listPlay=listSel;
                proVideo(listSel.get(0).getPath());
                ProActivity.proStatus=1;
                finish();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (videoAdapter.getItem(position).isSelect()) {
            videoAdapter.getItem(position).setSelect(false);
            listSel.remove(videoAdapter.getItem(position));
        } else {
            videoAdapter.getItem(position).setSelect(true);
            listSel.add(videoAdapter.getItem(position));
        }
        videoAdapter.notifyItemChanged(position);
    }

    @Override
    protected void initData() {
        getPermission();
    }

    //视频投屏
    private void proVideo(String url) {
        LelinkPlayerInfo playerInfo = new LelinkPlayerInfo();
        playerInfo.setType(LelinkSourceSDK.MEDIA_TYPE_VIDEO);
        Uri uri = Uri.parse(url);
        if (UriUtil.isContentSchemeUri(uri)) {
            playerInfo.setLocalUri(uri);
        } else {
            playerInfo.setLocalPath(url);
        }
        mCastScreenManager.startPlay(playerInfo);
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO).reqPermissionDesc(getString(R.string.get_storage_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                getData();
            }

            @Override
            public void onDenied(boolean needGoSettings) {
                mDataBinding.rvSelVideoList.setVisibility(View.GONE);
                mDataBinding.ivSelVideoNoData.setVisibility(View.VISIBLE);
            }
        }).request();
    }

    //获取视频和图片数据
    private void getData() {
        listShow.clear();
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    for (SelectMediaEntity bean : selectMediaEntities) {
                        listShow.add(new MyVideoBean(bean.getMediaName(), bean.getPath(), bean.getUri(), bean.getDuration(), FileUtils.getFileLastModified(bean.getPath()), bean.getSize(), false));
                    }
                    videoAdapter.setList(listShow);
                    mDataBinding.rvSelVideoList.setVisibility(View.VISIBLE);
                    mDataBinding.ivSelVideoNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvSelVideoList.setVisibility(View.GONE);
                    mDataBinding.ivSelVideoNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}